package com.example.securityconfig.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)  //开启链式编程
public class LoginUser implements UserDetails {
 
    /*
    * 自定义用户对象
    * */
    private User users;

    /*
     * 权限信息集合
     * */
    private List<String> authorityList;


    /*
    * 权限信息
    * */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 将权限信息集合封装为GrantedAuthority集合
        List<GrantedAuthority> grantedAuthorityList = new ArrayList<>();
        authorityList.forEach((authorityStr) -> {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(authorityStr);
            grantedAuthorityList.add(simpleGrantedAuthority);
        });
        return grantedAuthorityList;
    }
 
    /*
    * 密码
    * */
    @Override
    public String getPassword() {
        return users.getPassword();
    }
 
    /*
    * 用户名
    * */
    @Override
    public String getUsername() {
        return users.getUsername();
    }
 
    /*
    * 表示判断账户是否过期
    * */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
 
    /*
    * 表示判断账户是否被锁定
    * */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
 
    /*
    * 表示凭证{密码}是否过期
    * */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
 
    /*
    * 是否可用
    * */
    @Override
    public boolean isEnabled() {
        return true;
    }
}